package Person_JavaBean;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class User {
    private long id;
    private String name;
    private int defence;
    private int forc;
    private int inteligence;
    private int politics;
    private int charm;
    private String biolography;
    private InputStream photo;

    public User() {
    }

    public User(long id, String name, int defence, int forc, int inteligence, int politics, int charm, String biolography,InputStream photo) {
        this.id = id;
        this.name = name;
        this.defence = defence;
        this.forc = forc;
        this.inteligence = inteligence;
        this.politics = politics;
        this.charm = charm;
        this.biolography = biolography;
        this.photo = photo;
    }

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getDefence() {
        return defence;
    }

    public int getForc() {
        return forc;
    }

    public int getInteligence() {
        return inteligence;
    }

    public int getPolitics() {
        return politics;
    }

    public int getCharm() {
        return charm;
    }

    public InputStream getPhoto() {
        return photo;
    }

    public String getBiolography() {
        return biolography;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDefence(int defence) {
        this.defence = defence;
    }

    public void setForc(int forc) {
        this.forc = forc;
    }

    public void setInteligence(int inteligence) {
        this.inteligence = inteligence;
    }

    public void setPolitics(int politics) {
        this.politics = politics;
    }

    public void setCharm(int charm) {
        this.charm = charm;
    }

    public void setBiolography(String biolography) {
        this.biolography = biolography;
    }

    public void setPhoto(InputStream photo) {
        this.photo = photo;
    }

    public User findInfoByname(String name){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
            conn = JdbcUtil.getConnection();
            String sql = "select * from db where name = ?;";
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            if (rs.next()){
                user = new User(rs.getLong(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getBinaryStream(9));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtil.free(rs,ps,conn);
        }
        return user;
    }
}
